﻿$(document).ready(function () {
    $.fn.dstextbox = function (method, args) {
        var con = this;
        return $.controlInit("dstextbox", con, method, args);
    }
    $.fn.dstextbox.defaultOptions = {
        "width": 100,
        "height": 18,
        "icon": "",
        "iconTitle": "",
        "onIconClick": null,
        "onEnter": null
    };
    var methods = $.fn.dstextbox.methods = {
        init: function (ctl, args) {
            ctl.each(function (i, el) {
                var jq = $(el);
                var options = null;
                if (!jq.data("textbox")) {
                    options = $.extend({}, $.fn.dstextbox.defaultOptions, args);
                    jq.data("options", options);
                }
                else {
                    options = methods.options(jq);
                    options = $.extend(options, args);
                    jq.data("options", options);
                }
                methods.draw(jq, options);
                jq.data("textbox", true);
            });
        },
        options: function (jq, args) {
            return jq.data("options");
        },
        draw: function (jq, args) {
            var options = args;
            if (jq.data("textbox")) {
                var box = jq.parent();
                box.replaceWith(jq);
                box.remove();
            }
            var span = $("<span class=\"dstextbox\"/>").appendTo(jq.parent());
            jq.addClass("ds_textbox").css({ "width": options.width, "height": options.height }).appendTo(span);
            var width = jq.outerWidth();
            if (options.icon) {
                var s = $("<span></span>").appendTo(span);
                var icon = $("<span class=\"" + options.icon + " dstextIcon\" />").appendTo(s); //
                icon.css("background-position", "3px 3px");
                if (options.iconTitle) {
                    icon.attr(options.iconTitle);
                }
                if ($.isFunction(options.onIconClick)) {
                    icon.bind("click", function (evt) {
                        options.onIconClick(jq.val());
                    });
                }
                width += icon.width();
            }
            span.width(width);
            if ($.isFunction(options.onEnter)) {
                jq.bind("keydown", function (evt) {
                    if (evt.keyCode.toString() == "13") {
                        options.onEnter(jq.val());
                        return false;
                    }
                })
            }
        }
    }
    var textbox = $("body").find(".ds-textbox");
    textbox.each(function (i, el) {
        var t = textbox.eq(i);
        var width = t.width();
        var height = t.height();
        var icon = t.attr("icon");
        var iconTitle = t.attr("iconTitle");
        var onIconClick = t.attr("onIconClick");
        var onEnter = t.attr("onEnter");
        var fn = function (val) {
            var f = eval(onIconClick);
            if ($.isFunction(f)) {
                f.call(null, val);
            }
        }
        var fn2 = function (val) {
            var f = eval(onEnter);
            if ($.isFunction(f)) {
                f.call(null, val);
            }
        }
        t.dstextbox({
            "width": width,
            "height": height,
            "icon": icon,
            "iconTitle": iconTitle,
            "onIconClick": fn,
            "onEnter": fn2
        });
    });
});